/*
 * JUnit Test for login GUI calling Controller methods:
 * checkPasswordSizeCorrect(), emailContainsAt(),
 * checkPasswordSame(), addUserToSystem(); 
 * 
 * Author: Daniel Henderson
 * TCSS 360 Spring 2013 
 * University of Washington
 * May 28th 2013
 */

package JUnitTests;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import Model.Controller;
import Model.User;

/**
 * Testing methods in the Controller class that are used by the GUI 
 * during login and registration.
 * 
 * @author Daniel Henderson
 * @version UWT Spring 2013, May 28th. 
 * @group 3-squared-software-systems
 */
public class LoginFunctionsTest {
	
	private User james;	
	private User jill;	
	private User judy;	
	private User jake;
	private User janas;
	private User jimmy;
	private User jeremy;
	private Controller controller;
	
	/**
	 * Construct List of Conferences and Users to test.
	 * 
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		
		//create new Controller
		controller = new Controller();
		
		//Normal Users
		james = new User("james1", "password", "james@yahoo.com", "james");		
		jill = new User("jill1", "password", "jill@yahoo.com", "jill");		
		judy = new User("judy1", "password", "judy@yahoo.com", "judy");		
		jake = new User("jake1", "password", "jake@yahoo.com", "jake");		
		janas = new User("janas1", "password", "janas@yahoo.com", "janas");		
		jimmy = new User("jimmy1", "password", "jimmy@yahoo.com", "jimmy");
		jeremy = new User("jeremy1", "password", "jeremy@yahoo.com", "jeremy");
		
		//Add Users to Userlist
		controller.addUserToSystem(james);
		controller.addUserToSystem(jill);
		controller.addUserToSystem(judy);
		controller.addUserToSystem(jake);
		controller.addUserToSystem(janas);
		controller.addUserToSystem(jimmy);
		controller.addUserToSystem(jeremy);
		
	}
	
	/**
	 * Test method that will test the checkPasswordSame method
	 * inside the controller. This will test CASE sensitive strings
	 * against each other.
	 */
	@Test
	public void testPasswordSame() {
		
		//Test to be sure that CASE matters
		assertTrue(controller.checkPasswordSame(james.getPassword(), judy.getPassword()));
		assertFalse(controller.checkPasswordSame("THIS", "this"));
		assertTrue(controller.checkPasswordSame("THIS", "THIS"));
		
	}
	
	/**
	 * Test method that will check an email adress and be sure
	 * its valid. Check for @ symbol, check for character count 
	 * greater or equal to 3.
	 */
	@Test
	public void testEmailString() {
		
		//Test to be sure password is right length, has period, and AT Symbol
		assertTrue(controller.emailContainsAt("admin@admin.com")); //Test correct address
		assertFalse(controller.emailContainsAt("admin")); //Test no AT symbol
		assertFalse(controller.emailContainsAt("@")); //Test Only AT symbol
		assertFalse(controller.emailContainsAt("1@")); //Test no period by has AT
		assertTrue(controller.emailContainsAt("1@b.com")); //Test has all
		assertTrue(controller.emailContainsAt("1@b.c")); //Test has all
		assertTrue(controller.emailContainsAt(jeremy.getEmail())); //Test get email method for controller
		
	}
	
	/**
	 * Test method that will check a password to be sure
	 * it is atleast 8 characters and contains no spaces.
	 */
	@Test
	public void testPasswordLength() {
		assertTrue(controller.checkPasswordSizeCorrect("12345678")); //Test 8 chars
		assertFalse(controller.checkPasswordSizeCorrect("1234567")); //Test less then 7 chars
		assertTrue(controller.checkPasswordSizeCorrect(jake.getPassword())); //Test get password method
		assertFalse(controller.checkPasswordSizeCorrect("        ")); //Test spaces not allowed
	}
	
	/**
	 * Test method that will try to add a User to the system
	 * that either should already exist (aka same login), or
	 * one that should not exists in the system and the 
	 * method should return true saying User was added properly.
	 */
	@Test
	public void testAddUserToSystem() {
		assertTrue(controller.addUserToSystem(new User("james", "12345678", "james@yahoo.com", "james"))); //Should add to sys
		assertFalse(controller.addUserToSystem(new User("james1", "12345678", "james@yahoo.com", "james"))); //Should alrdy exist
		assertFalse(controller.addUserToSystem(new User("James1", "12345678", "james@yahoo.com", "james"))); //Case should not matter
		assertFalse(controller.addUserToSystem(new User("admin", "12345678", "james@yahoo.com", "james"))); //Should alrdy exist
	}

}
